package  
{
	/**
	 * 简单预加载
	 * @author Ning
	 */
	import br.com.stimuli.loading.BulkProgressEvent;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.getDefinitionByName;
	import ginger.utils.NetUtil;
	import ginger.utils.ResourecsPool;
	
	/**
	 * ...
	 * @author Ning
	 */
	public class Preloader extends MovieClip 
	{
		private var _configUrl:String;
		private var _urlloader:URLLoader;
		private var _resourceXMLLoader:URLLoader;
		private var _resourcePool:ResourecsPool;
		
		public static const RESOURCE_POOL_NAME:String = 'ResourecsPool';

		
		public function Preloader() 
		{
			if (stage) {
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
			}
			addEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			// TODO show loader
			
			_resourcePool =  new ResourecsPool(RESOURCE_POOL_NAME);
			
			AppGlobal.RESOURES_POOL = _resourcePool;
			
			
			//设置配置路径
			var o:Object = NetUtil.getFlashVarsParam(this.root, 'config');
			_configUrl = o == null?'xml/webconfig.xml':String(o);

			_urlloader = new URLLoader();
			_urlloader.addEventListener(Event.COMPLETE, onConfigComplete);
		}
		
		private function ioError(e:IOErrorEvent):void 
		{
			trace(e.text);
		}
		
		private function progress(e:ProgressEvent):void 
		{
			// TODO update loader
		}
		
		private function checkFrame(e:Event):void 
		{
			if (currentFrame == totalFrames) 
			{
				stop();
				loadingFinished();
				//配置载入
				_urlloader.load(new URLRequest(_configUrl));
			}
		}
		private function onConfigComplete(evt:Event):void
		{
			var configXML:XML = XML(evt.target.data);
			
			AppGlobal.CONFIG_XML = configXML;
			
			_resourceXMLLoader = new URLLoader();
			_resourceXMLLoader.addEventListener(Event.COMPLETE, onResourceXMLComplete);
			_resourceXMLLoader.load(new URLRequest(String(configXML.base.@['resourecs'])));
			
			
			
			
		}
		private function onResourceXMLComplete(evt:Event):void
		{
			var resourcesXML:XML = XML(evt.target.data);
			var configXML:XML = AppGlobal.CONFIG_XML;
			
			//添加素材资源
			_resourcePool.setConfigXML(resourcesXML);
			AppGlobal.MODULE_NAME = [];
			var moduleXml:XMLList = configXML.modules.module;
			for (var i:int = 0; i < moduleXml.length(); i++ )
			{ 
				_resourcePool.add(String(moduleXml[i].@['url']), { id:String(moduleXml[i].@['name']), context:new LoaderContext(false, new ApplicationDomain()) } );
			    AppGlobal.MODULE_NAME.push(String(moduleXml[i].@['name']));
			}
			
			//添加数据
			_resourcePool.add(String(configXML.base.@['data']), { id:AppGlobal.DATA_KEY } );
			_resourcePool.add(String(configXML.base.@['layout']), { id:AppGlobal.LAYOUT_KEY } );

			_resourcePool.addEventListener(BulkProgressEvent.COMPLETE, onResourceComplete);
			_resourcePool.start();
		}
		private function onResourceComplete(evt:BulkProgressEvent):void
		{
			var mresources:MaterialResources = new MaterialResources;
			AppGlobal.MATERIAL_RESOURCES = mresources;
			
			_resourcePool.updateResourecs(mresources);
			
			startup();
		}
		private function loadingFinished():void 
		{
			removeEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
			// TODO hide loader
		}
		
		private function startup():void 
		{
			var mainClass:Class = getDefinitionByName("Application") as Class;
			if (parent == stage) stage.addChildAt(new mainClass() as DisplayObject, 0);
			else addChildAt(new mainClass() as DisplayObject, 0);
		}
	}
}